package com.gym.controller.form.customer;

import java.sql.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class FormRegistrazione extends ActionForm{

	private String email;
	private String password;
	private String passwordRepeat;
	private String code;
	private String firstname;
	private String lastname;
	private String gender;
	private String fc;
	private String phone;
	private Date bdate;
	private Date cdatexpiry;
	private Date idate;

	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getFc() {
		return fc;
	}
	public void setFc(String fc) {
		this.fc = fc;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public Date getBdate() {
		return bdate;
	}
	public void setBdate(Date bdate) {
		this.bdate = bdate;
	}
	public Date getCdatexpiry() {
		return cdatexpiry;
	}
	public void setCdatexpiry(Date cdatexpiry) {
		this.cdatexpiry = cdatexpiry;
	}
	public Date getIdate() {
		return idate;
	}
	public void setIdate(Date idate) {
		this.idate = idate;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPasswordRepeat() {
		return passwordRepeat;
	}
	public void setPasswordRepeat(String passwordRepeat) {
		this.passwordRepeat = passwordRepeat;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,	HttpServletRequest request) {

		ActionErrors errors= new ActionErrors();

		if(email==null || email.isEmpty())
			errors.add("email" , new ActionMessage("it.errore.email.obbligatorio"));
		else if( ! email.matches("[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+.[a-zA-Z]{2,4}"))
			errors.add("email", new ActionMessage("it.errore.email.formato"));


		if(getPassword()==null || getPassword().isEmpty())
			errors.add("password" , new ActionMessage("it.errore.password.obbligatorio"));
		else if(getPassword().length()<8)
			errors.add("password" , new ActionMessage("it.errore.password.corta"));

		if(getPasswordRepeat()==null || getPasswordRepeat().isEmpty())
			errors.add("passwordRepeat" , new ActionMessage("it.errore.passwordRepeat.obbligatorio"));
		else if(! getPasswordRepeat().equals(getPassword()))
			errors.add("passwordRepeat" , new ActionMessage("it.errore.passwordRepeat.diversa"));
		

		if(getFirstname()==null || getFirstname().isEmpty()){
			errors.add("firstname", new ActionMessage("it.errore.nome.obbligatorio"));}

		if(getLastname()==null || getLastname().isEmpty()){
			errors.add("lastname", new ActionMessage("it.errore.cognome.obbligatorio"));}

		if(getFc()==null || getFc().length()!=16 || ! getFc().matches("[a-zA-Z]{6}\\d\\d[a-zA-Z]\\d\\d[a-zA-Z]\\d\\d\\d[a-zA-Z]")){
			errors.add("fc", new ActionMessage("it.errore.fc.formato"));}

		if(getBdate()==null)
			errors.add("bdate", new ActionMessage("it.errore.bdate.obbligatorio"));
		
		if(getCdatexpiry()==null)
			errors.add("cdatexpiry", new ActionMessage("it.errore.cdatexpiry.obbligatorio"));
		
		return errors;

	}

}
